home *** CD-ROM | disk | FTP | other *** search
-
- #include "MyHeader.h"
-
- // static function prototypes
- static void HandleEvent(EventRecord * event);
- static void HandleMenu(long selection);
- static void HandleQuit(void);
- static void HandleKey(EventRecord * event);
- static void PerFrameKeyCheck(void);
-
- GlobalState globalState;
-
- // code
- void main(void)
- {
- EventRecord theEvent;
-
- /*****/
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- SetMenuBar(GetNewMBar(menuBarID));
- AppendResMenu(GetMenuHandle(appleMenuID), 'DRVR');
- DrawMenuBar();
-
- MyTestSetup();
-
-
- do{
- WaitNextEvent(everyEvent, &theEvent, 0, NULL);
- HandleEvent(&theEvent);
- } while (1);
- }
-
-
-
- static void HandleEvent(EventRecord * event)
- {
- WindowPtr theWindow;
- static Rect growRect = {100, 100, 32000, 32000};
- long newWindowSize;
-
- /***********/
-
- switch(event->what){
- case autoKey:
- case keyDown:
- if(event->modifiers & cmdKey){
- HandleMenu(MenuKey(event->message & charCodeMask));
- } else {
- HandleKey(event);
- }
- break;
- case mouseDown:
- switch(FindWindow(event->where, &theWindow)){
- case inMenuBar:
- HandleMenu(MenuSelect(event->where));
- break;
- case inDrag:
- DragWindow(theWindow, event->where, &(**GetGrayRgn()).rgnBBox);
- WindowChanged(theWindow);
- break;
- case inGrow:
- newWindowSize = GrowWindow(theWindow, event->where, &growRect);
- SizeWindow(theWindow, LoWord(newWindowSize), HiWord(newWindowSize), true);
- WindowChanged(theWindow);
- break;
- case inContent:
- SelectWindow(theWindow);
- break;
- case inGoAway:
- if(TrackGoAway(theWindow, event->where)){
- ClosingWindow(theWindow);
- CloseWindow(theWindow);
- }
- break;
- }
- break;
- case updateEvt:
- theWindow = (WindowPtr)event->message;
- BeginUpdate(theWindow);
- EndUpdate(theWindow);
- WindowChanged(theWindow); // this is incase the bit depth changed
- DrawFrame((MyState*)GetWRefCon(theWindow));
- break;
- case activateEvt:
- theWindow = (WindowPtr)event->message;
-
- if(event->modifiers & activeFlag){
- UpdateMyMenus(theWindow);
- }
-
- break;
- case nullEvent:
- PerFrameKeyCheck();
- theWindow = FrontWindow();
- while(theWindow){
- MyState * state = (MyState*)GetWRefCon(theWindow);
- if(state->active)
- DrawFrame(state);
-
- theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
- }
- break;
- }
- }
-
-
- static void HandleMenu(long selection)
- {
- short menuID = selection >> 16;
- short itemID = selection;
- WindowPtr w;
-
- Str255 itemName;
-
- ///////////
-
- if(menuID == appleMenuID && itemID > 2){
- GetMenuItemText(GetMenuHandle(appleMenuID), itemID, itemName);
- OpenDeskAcc(itemName);
- }
-
-
- switch(menuID){
- case fileMenuID:
- switch(itemID){
- case 1:
- SetupNewWindow();
- break;
- case 3:
- w = FrontWindow();
- if(w){
- ClosingWindow(w);
- CloseWindow(w);
- }
- break;
- case 6:
- HandleQuit();
- break;
- }
- break;
- case engineMenuID:
- case optionsMenuID:
- case antialiasingMenuID:
- case textureFilterMenuID:
- HandleOtherMenu(menuID, itemID);
- break;
- }
-
- HiliteMenu(0);
- }
-
- static void HandleQuit(void)
- {
- WindowPtr w;
-
- /********/
-
- while(w = FrontWindow()){
- ClosingWindow(w);
- CloseWindow(w);
- }
-
- UnloadTextures();
-
- ExitToShell();
- }
-
-
-
- #define rotatePerKey 0.1
- #define movePerKey 0.5
-
- static void HandleKey(EventRecord * event)
- {
- // MyMatrix m;
-
- unsigned char theKey = (event->message & keyCodeMask) >> 8;
- unsigned char theChar = event->message & charCodeMask;
- switch(theChar){
- case charESC:
- globalState.stopObjects = !globalState.stopObjects;
- break;
- /*
- case charUp:
- globalState.camera.w.x += globalState.camera.z.x * movePerKey;
- globalState.camera.w.y += globalState.camera.z.y * movePerKey;
- globalState.camera.w.z += globalState.camera.z.z * movePerKey;
- break;
- case charDown:
- globalState.camera.w.x += globalState.camera.z.x * -movePerKey;
- globalState.camera.w.y += globalState.camera.z.y * -movePerKey;
- globalState.camera.w.z += globalState.camera.z.z * -movePerKey;
- break;
- case charLeft:
- MyMatrixSetRotateY(rotatePerKey, &m);
- MyMatrixRotateByMatrix(&globalState.camera, &m);
- break;
- case charRight:
- MyMatrixSetRotateY(-rotatePerKey, &m);
- MyMatrixRotateByMatrix(&globalState.camera, &m);
- break;
- */
- default:
- globalState.unusedKey = theChar;
- }
- globalState.unusedKey = theKey;
- }
-
-
- #define IsKeyDown(keyMap, theKey) (((unsigned char *)(keyMap))[(theKey) / 8] & 1 << ((theKey) % 8))
-
- #define keyLeft 0x7b
- #define keyRight 0x7c
- #define keyUp 0x7e
- #define keyDown 0x7d
- #define keyCommand 55
- #define keyOption 58
- #define keyShift 56
- #define keyControl 63
- #define keySpace 0x31
-
-
- static void PerFrameKeyCheck(void)
- {
- KeyMap theKeys;
- MyMatrix m, m2;
-
- int command, option;
-
- /*********/
-
- GetKeys(theKeys);
-
- command = IsKeyDown(theKeys, keyCommand);
- option = IsKeyDown(theKeys, keyOption);
-
- if(IsKeyDown(theKeys, keySpace)){
- globalState.roll = 0;
- globalState.pitch = 0;
- globalState.yaw = 0;
- MyMatrixClear(&globalState.camera);
- }
- if(IsKeyDown(theKeys, keyLeft)){
- if(command){
- globalState.camera.w.x += globalState.camera.x.x * movePerKey;
- globalState.camera.w.y += globalState.camera.x.y * movePerKey;
- globalState.camera.w.z += globalState.camera.x.z * movePerKey;
- } else if (option) {
- globalState.roll -= rotatePerKey;
- } else {
- globalState.yaw += rotatePerKey;
- }
- }
-
- if(IsKeyDown(theKeys, keyRight)){
- if(command){
- globalState.camera.w.x += globalState.camera.x.x * -movePerKey;
- globalState.camera.w.y += globalState.camera.x.y * -movePerKey;
- globalState.camera.w.z += globalState.camera.x.z * -movePerKey;
- } else if (option) {
- globalState.roll += rotatePerKey;
- } else {
- globalState.yaw -= rotatePerKey;
- }
- }
-
- if(IsKeyDown(theKeys, keyUp)){
- if(command){
- globalState.camera.w.x += globalState.camera.y.x * movePerKey;
- globalState.camera.w.y += globalState.camera.y.y * movePerKey;
- globalState.camera.w.z += globalState.camera.y.z * movePerKey;
- } else if (option) {
- globalState.pitch -= rotatePerKey;
- } else {
- globalState.camera.w.x += globalState.camera.z.x * movePerKey;
- globalState.camera.w.y += globalState.camera.z.y * movePerKey;
- globalState.camera.w.z += globalState.camera.z.z * movePerKey;
- }
- }
-
- if(IsKeyDown(theKeys, keyDown)){
- if(command){
- globalState.camera.w.x += globalState.camera.y.x * -movePerKey;
- globalState.camera.w.y += globalState.camera.y.y * -movePerKey;
- globalState.camera.w.z += globalState.camera.y.z * -movePerKey;
- } else if (option) {
- globalState.pitch += rotatePerKey;
- } else {
- globalState.camera.w.x += globalState.camera.z.x * -movePerKey;
- globalState.camera.w.y += globalState.camera.z.y * -movePerKey;
- globalState.camera.w.z += globalState.camera.z.z * -movePerKey;
- }
- }
-
- MyMatrixSetRotateZ(globalState.roll, &m);
-
- MyMatrixSetRotateX(globalState.pitch, &m2);
- MyMatrixRotateByMatrix(&m, &m2);
-
- MyMatrixSetRotateY(globalState.yaw, &m2);
- MyMatrixRotateByMatrix(&m, &m2);
-
- globalState.camera.x = m.x;
- globalState.camera.y = m.y;
- globalState.camera.z = m.z;
-
-
-
- }
-
-
-